// pages/oa/approval/index.js
var api = require("../../../utils/api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbar: ['审批中', '已完成'],
    currentTab: 0,
    statuslist: [
      // { id: "0", text: "业务审批", amount: "0", show: "Y" },
      // { id: "1", text: "用印审批", amount: "0", show: "Y" },
      { id: "2", text: "财务审批", amount: "0", show: "Y" },
      // { id: "3", text: "人事审批", amount: "0", show: "Y" },
      // { id: "4", text: "行政审批", amount: "0", show: "Y" },
    ],
    statuslist1: [
      // { id: "0", text: "业务审批", amount: "0", show: "Y" },
      // { id: "1", text: "用印审批", amount: "0", show: "Y" },
      { id: "2", text: "财务审批", amount: "0", show: "Y" },
      // { id: "3", text: "人事审批", amount: "0", show: "Y" },
      // { id: "4", text: "行政审批", amount: "0", show: "Y" },
    ],
    uid: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var orgid = wx.getStorageSync('companyId');
    var uid = wx.getStorageSync('userId');
    this.setData({
      orgid: orgid,
      uid: uid,
    })

  },
  getApplyInfoCount: function (msg) {
    var that = this;
    wx.showLoading({
      title: msg,
    })
    api.fetchApi2("oaApproval/getApplyInfoCount?userId=" + that.data.uid + "&orgid=" + that.data.orgid, {
    }, "POST").then(res => {
      wx.hideLoading();
      var datas = res.data;
      var statuslist = that.data.statuslist;
      // statuslist[0]["amount"] = datas.ywCount;
      // statuslist[1]["amount"] = datas.printCount;
      statuslist[0]["amount"] = datas.cwCount;
      // statuslist[3]["amount"] = datas.rsCount;
      // statuslist[4]["amount"] = datas.xzCount;
      that.setData({
        statuslist: statuslist,
      })
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    });
  },

  getCompApplyInfoCount: function (msg) {
    var that = this;
    wx.showLoading({
      title: msg,
    })
    api.fetchApi2("oaApproval/getCompApplyInfoCount?userId=" + that.data.uid + "&orgid=" + that.data.orgid, {
    }, "POST").then(res => {
      wx.hideLoading();
      var datas = res.data;
      var statuslist = that.data.statuslist;
      // statuslist[0]["amount"] = datas.ywCount;
      // statuslist[1]["amount"] = datas.printCount;
      statuslist[0]["amount"] = datas.cwCount;
      // statuslist[3]["amount"] = datas.rsCount;
      // statuslist[4]["amount"] = datas.xzCount;
      that.setData({
        statuslist1: statuslist,
      })
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getApplyInfoCount("正在加载数据...");
    this.getCompApplyInfoCount("正在加载数据...");
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //响应点击导航栏
  navbarTap: function (e) {
    var that = this;
    var idx = e.currentTarget.dataset.idx;
    if (idx == 0) {
      that.getApplyInfoCount();
    } else if (idx == 1) {
      that.getCompApplyInfoCount();

    }
    that.setData({
      currentTab: e.currentTarget.dataset.idx,
      TypeItem: that.data.navbar[that.data.currentTab]
    })
  },
  showDetail: function (e) {
    var that = this;
    var idx = e.currentTarget.id;
    var currentTab = that.data.currentTab;
    //进入待审批详情
    if (currentTab == 0) {
      wx.navigateTo({
        url: 'list?id=' + idx
      });
    }
    //进入已审批详情
    if (currentTab == 1) {
      wx.navigateTo({
        url: 'listcomp?id=' + idx
      });
    }
  },
})